﻿Imports System
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Public Class LibDataBoxPicker
    Inherits DateTimePicker


    Private Const WM_LBUTTONDOWN As Int32 = &H201

    <DllImport("user32.dll")> _
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal uMsg As UInt32, ByVal wParam As Int32, ByVal lParam As Int32) As IntPtr
    End Function

    Friend WithEvents _DateBox As LibDateBox

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub InitializeComponent()
        Me._DateBox = New LibDateBox

        '
        'LibDateBoxPicker
        '
        Me.Size = New System.Drawing.Size(100, 25)
        Me.ResumeLayout(False)
        Me.PerformLayout()
        Me.Controls.Add(Me._DateBox)

        Me.SuspendLayout()
        '
        '_DateBox
        '
        Me._DateBox.CausesValidation = False
        Me._DateBox.EnterDateNow = True
        Me._DateBox.FormatString = "dd/MM/yyyy"
        Me._DateBox.Location = New System.Drawing.Point(0, 0)
        Me._DateBox.Mask = "00/00/0000"
        Me._DateBox.Name = "_DateBox"
        Me._DateBox.SelectOnEnter = True
        Me._DateBox.Size = New System.Drawing.Size(100, 20)
        Me._DateBox.TabIndex = 0
        Me._DateBox.ValidatingType = GetType(Date)
        Me.Controls.Add(_DateBox)
    End Sub

    Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
        Me._DateBox.Visible = True
        Me._DateBox.Bounds = New Rectangle(Me.Bounds.X, Me.Bounds.Y, Me.Bounds.Width, Me.Bounds.Height)
        MyBase.OnResize(e)
    End Sub

    Protected Sub _DateBox_OnDoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles _DateBox.DoubleClick
        Dim x As Int32 = Me.Width - 10
        Dim y As Int32 = CInt(Me.Height / 2)
        Dim lParam As Int32 = x + y * &H10000
        SendMessage(Me.Handle, WM_LBUTTONDOWN, 1, lParam)
    End Sub

    Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)
        Me._DateBox.Visible = True
        Me._DateBox.BringToFront()
        Me._DateBox.Bounds = pevent.ClipRectangle()
        MyBase.OnPaintBackground(pevent)
    End Sub
End Class
